package com.gee.lowui;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import com.gee.lowui.Button;
import com.gee.lowui.GControl;

public class VectorMenu extends GControl
{
	public byte state = 0;

	final public static byte//
			VMENU_CLOSE = 0,//
			VMENU_CLOSEING = 1,//
			VMENU_OPENING = 2,//
			VMENU_OPEN = 3;

	final public static byte //
			WAY_UP = 0,//
			WAY_DOWN = 1,//
			WAY_LEFT = 2,//
			WAY_RIGHT = 3;

	public String[] strMenu;

	private Button[] but;

	public int[][] start;

	public int[] end = { 240, 320 };

	public int step = 5;

	public int walk = 0;

	public int[][][] vecWalk;

	public VectorMenu(Image[] img, String[] strMenu)
	{
		but = new Button[9];
		for (int i = 0; i < but.length; i++)
		{
			but[i] = new Button(img, i < strMenu.length ? strMenu[i] : "");
			but[i].setAnchor(Graphics.TOP | Graphics.LEFT);
			but[i].setPostion(end[0], end[1]);
		}
		controlW = end[0] - this.x;
		controlH = end[1] - this.y;
	}

	public int getControlType()
	{
		return GControl.CONTROL_VECTORMENU;
	}

	public void initVector()
	{
		start = new int[9][2];
		int count = 0;
		for (int iy = 0; iy < 3; iy++)
		{
			for (int ix = 0; ix < 3; ix++)
			{
				start[count][0] = x + controlW / 3 * ix;
				start[count][1] = y + controlH / 3 * iy;
				count++;
			}
		}
		vecWalk = new int[9][step][2];
		for (int i = 0; i < 9; i++)
		{
			for (int j = 0; j < step; j++)
			{
				vecWalk[i][j][0] = (end[0] - start[i][0]) / step * (j + 1) + start[i][0];
				vecWalk[i][j][1] = (end[1] - start[i][1]) / step * (j + 1) + start[i][1];
			}
		}
		setIndex(index);
	}

	public void setIndex(int ie)
	{
		index = ie;
		for (int i = 0; i < but.length; i++)
		{
			but[i].setAction(false);
		}
		but[index].setAction(true);
		controlString = but[index].controlString;
	}

	public void move(int way)
	{
		if (state == VMENU_OPEN)
		{
			switch (way)
			{
				case WAY_LEFT:
					index--;
					if ((index + 1) % 3 == 0)
					{
						index += 3;
					}
					break;
				case WAY_RIGHT:
					index++;
					if ((index) % 3 == 0)
					{
						index -= 3;
					}
					break;
				case WAY_UP:
					index -= 3;
					if (index < 0)
					{
						index += 9;
					}
					break;
				case WAY_DOWN:
					index += 3;
					if (index > 8)
					{
						index -= 9;
					}
					break;
			}
			setIndex(index);
			controlString = but[index].controlString;
		}
	}

	public void drawControl(Graphics g)
	{
		g.setColor(0x000000);
		switch (state)
		{
			case VMENU_CLOSE:
				for (int i = 0; i < start.length; i++)
				{
					g.drawRect(end[0], end[1], 60, 20);
					but[i].setPostion(end[0], end[1]);
				}
				break;
			case VMENU_OPEN:
				for (int i = 0; i < start.length; i++)
				{
					g.drawRect(start[i][0], start[i][1], 60, 20);
					but[i].setPostion(start[i][0], start[i][1]);
				}
				break;
			case VMENU_CLOSEING:
				for (int i = 0; i < vecWalk.length; i++)
				{
					g.drawRect(vecWalk[i][walk][0], vecWalk[i][walk][1], 60, 20);
					but[i].setPostion(vecWalk[i][walk][0], vecWalk[i][walk][1]);
				}
				walk++;
				if (walk >= step)
				{
					state = VMENU_CLOSE;
				}
				break;
			case VMENU_OPENING:
				for (int i = 0; i < vecWalk.length; i++)
				{
					g.drawRect(vecWalk[i][walk][0], vecWalk[i][walk][1], 60, 20);
					but[i].setPostion(vecWalk[i][walk][0], vecWalk[i][walk][1]);
				}
				walk--;
				if (walk <= 0)
				{
					state = VMENU_OPEN;
				}
				break;
		}
		for (int i = 0; i < but.length; i++)
		{
			but[i].drawControl(g);
		}
	}

	public void setState(byte s)
	{
		if (state != s)
		{
			state = s;
			switch (state)
			{
				case VMENU_CLOSEING:
					walk = 0;
					break;
				case VMENU_OPENING:
					walk = step - 1;
					break;
			}
		}
	}
}